package com.simoniu.first;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TestServlet", value = "/test")
public class TestServlet extends HttpServlet {

    public TestServlet() {
        System.out.println("执行TestServlet的构造方法...");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String city=null;
        String sessionId = null;
        String date = null;
        //获取request内保存的城市。
        if(request.getAttribute("city")!=null) {
            city = (String) request.getAttribute("city");
        }
        //因为使用了请求重定向，不是一次请求了，所有这里输出是null.
        System.out.println("request内的city=" + city);

        //获取session内保存的会话编号
        if(request.getSession().getAttribute("sessionId")!=null){
            sessionId = (String) request.getSession().getAttribute("sessionId");
        }
        System.out.println("session内的会话编号=" + sessionId);

        //获取application内保存的日期
        if(request.getServletContext().getAttribute("date")!=null){
            date = (String) request.getServletContext().getAttribute("date");
        }

        System.out.println("application内的日期=" + date);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
